<template>
	<view>
		<uni-header title="产品详情"></uni-header>
		<view class="info">
			<text class="title">{{detail.title}}</text>
			<text class="center">{{detail.expectedIncome}}</text>
			<text class="bottom">预计收益</text>
		</view>
		<view class="list" v-if="detail">
			<view class="item">
				<image src="../../static/jiage.png" mode=""></image>
				<view class="">
					<text class="title">¥{{detail.unitPrice}}</text>
					<text class="desc">每份价格</text>
				</view>
			</view>
			<view class="item">
				<image src="../../static/suanli.png" mode=""></image>
				<view class="">
					<text class="title">{{detail.computingPower}}T</text>
					<text class="desc">每份算力</text>
				</view>
			</view>
			<view class="item">
				<image src="../../static/qixian.png" mode=""></image>
				<view class="">
					<text class="title">{{detail.lockPeriod}}天</text>
					<text class="desc">期限</text>
				</view>
			</view>
			<view class="item">
				<image src="../../static/duihuan.png" mode=""></image>
				<view class="">
					<text class="title">T+1到账，自行兑换</text>
					<text class="desc">结算方式</text>
				</view>
			</view>
		</view>
		<button class="submit" @click="showlog">购买</button>
		<view class="model" v-if="modelState">
			<view class="content">
				<view class="header">
					<text>购买产品</text>
					<image src="../../static/close.png" @click="hideModel" mode=""></image>
				</view>
				<view class="top">
					<text class="title">{{detail.title}}</text>
					<text class="desc">¥{{detail.unitPrice}}/份</text>
				</view>
				<text class="label">购买数量</text>
				<view class="input">
					<input type="number" @input="getInput" />
					<text class="unit">台</text>
				</view>
				<text class="label">合计</text>
				<text class="price">¥{{price}}</text>
				<button class="submit" @click="pay">支付</button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		ref
	} from 'vue';
	import {
		productInfo,
		transactionSave,
		transactionUpdate
	} from '@/util/api.js'
	const props = defineProps(["id"])
	const detail = ref({})
	const price = ref(0)
	const num = ref()
	onMounted(() => {
		getDetail()
	})
	const getDetail = async () => {
		const result = await productInfo(props.id)
		detail.value = result.data
		console.log(detail.value)
	}

	const modelState = ref(false)
	const showlog = () => {
		modelState.value = true
	}
	const hideModel = () => {
		modelState.value = false
	}
	const getInput = (e)=>{e.detail.value
		num.value = e.detail.value
		price.value = (num.value * detail.value.unitPrice).toFixed(2)
	}
	const pay = async ()=>{
		uni.showToast({
			icon:"none",
			title:"余额不足"
		})
		return false
		if(price.value>0){
			await transactionSave({
				productId:props.id,
				productNum:num.value,
				transactionType:3
			})
			await transactionUpdate({
				productId:props.id,
				productNum:num.value,
				transactionType:3
			})
			
		}else{
			uni.showToast({
				icon:"none",
				title:"输入正确购买数量"
			})
		}
	}
</script>

<style lang="scss" scoped>
	.model {
		position: fixed;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		background: rgba(0, 0, 0, 0.5);
		z-index: 1;

		.content {
			width: 750rpx;
			position: absolute;
			background: #FFFFFF;
			border-radius: 40rpx 40rpx 0px 0px;
			top: 220rpx;
			bottom: 0;

			.price {
				display: block;
				color: #181D29;
				font-size: 64rpx;
				font-weight: 500;
				margin-left: 64rpx;
				margin-top: 32rpx;
			}

			.input {
				width: 622rpx;
				height: 112rpx;
				border-bottom: 2rpx solid #E8EBF5;
				margin: 0 auto;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 64rpx;

				input {
					display: block;
					height: 112rpx;
					color: #181D29;
					font-size: 64rpx;
					font-weight: 500;
					flex: 2;
				}

				.unit {
					color: #181D29;
					font-size: 28rpx;
				}
			}

			.label {
				color: #181D29;
				font-size: 28rpx;
				display: block;
				margin-left: 64rpx;
			}

			.top {
				height: 216rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;

				.title {
					color: #181D29;
					font-size: 36rpx;
					font-weight: 500;
					display: block;
				}

				.desc {
					color: #181D29;
					font-size: 28rpx;
					text-align: center;
				}
			}

			.header {
				display: flex;
				height: 112rpx;
				padding: 0 32rpx;
				align-items: center;
				justify-content: space-between;

				image {
					display: block;
					width: 48rpx;
					height: 48rpx;
				}

				text {
					color: #181D29;
					font-size: 36rpx;
					font-weight: 500;
				}
			}
		}
	}

	.submit {
		width: 686rpx;
		height: 92rpx;
		background: #1E53E7;
		border-radius: 16rpx;
		position: absolute;
		bottom: 68rpx;
		left: 32rpx;
		color: #fff;
		font-size: 32rpx;
		font-weight: 500;
		line-height: 92rpx;
	}

	.list {
		padding: 0 32rpx;

		.item {
			width: 686rpx;
			height: 152rpx;

			display: flex;
			align-items: center;
			border-bottom: 2rpx solid #F2F4FA;

			image {
				display: block;
				width: 48rpx;
				height: 48rpx;
				margin-left: 48rpx;
			}

			view {
				margin-left: 48rpx;
			}

			.title {
				color: #181D29;
				font-size: 36rpx;
				font-weight: 500;
				display: block;
			}

			.desc {
				color: #A3A7B2;
				font-size: 28rpx;
			}
		}
	}

	.info {
		width: 622rpx;
		height: 248rpx;
		background: #FFFFFF;
		box-shadow: 0px 12rpx 32rpx 0px rgba(24, 32, 51, 0.06);
		border-radius: 24rpx;
		margin: 0 auto 32rpx;
		padding: 0 32rpx;
		overflow: hidden;

		.title {
			display: block;
			color: #3D3D3D;
			font-weight: 500;
			font-size: 36rpx;
			margin-top: 32rpx;
		}

		.center {
			color: #F53B47;
			font-weight: 500;
			display: block;
			font-size: 48rpx;
			margin: 24rpx 0 8rpx;
		}

		.bottom {
			display: block;
			color: #A3A7B2;
			font-size: 28rpx;
		}
	}
</style>